/**
 * File: ExpectedWeightedCovergeStrategy.java
 * Created by: mhaimel
 * Created on: 9 Jun 2009
 * CVS:  $Id: ExpectedWeightedCovergeStrategy.java,v 1.2 2009/11/24 15:53:19 mhaimel Exp $
 */
package uk.ac.ebi.velvet.strategy;

import static uk.ac.ebi.curtain.utils.StatisticHelper.avg;
import static uk.ac.ebi.curtain.utils.StatisticHelper.wCov;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import uk.ac.ebi.velvet.processor.ExpectedCoverageStrategy;

/**
 * @author mhaimel
 *
 */
public class ExpectedWeightedCovergeStrategy implements ExpectedCoverageStrategy {
	
	private final Integer minCov;
	public ExpectedWeightedCovergeStrategy() {
		this(5);
	}
	public ExpectedWeightedCovergeStrategy(Integer minCov) {
		this.minCov = minCov;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Integer calculateExpCov(ExpectedCoverageInformation info) {
		List<Integer> maxCov = new ArrayList<Integer>();
		long maxLen = -1;
		Integer minCutoff = info.getCoverageCutoff();
		if(minCutoff == null || minCutoff < this.minCov){
			minCutoff = this.minCov;
		}
		
		for(Entry<Integer, List<Integer>> entry : info.getCov2Score().entrySet()){
			if(entry.getKey() > minCutoff){
				long tmpLen = 
					wCov(entry.getValue(),entry.getKey()).longValue();
				if(tmpLen > maxLen){
					maxCov.clear();
					maxCov.add(entry.getKey());
					maxLen = tmpLen;
				} else if(tmpLen == maxLen){
					maxCov.add(entry.getKey());
				}
			}
		}
		int exp = avg(maxCov).intValue();
		return exp>= this.minCov?exp:this.minCov;
	}
}
